//package com.api_getway.api_getway.config.handler;
//
//import org.springframework.boot.autoconfigure.web.ErrorProperties;
//import org.springframework.boot.autoconfigure.web.ResourceProperties;
//import org.springframework.boot.autoconfigure.web.reactive.error.DefaultErrorWebExceptionHandler;
//import org.springframework.boot.web.reactive.error.ErrorAttributes;
//import org.springframework.context.ApplicationContext;
//import org.springframework.web.context.request.RequestAttributes;
//import org.springframework.web.reactive.function.server.*;
//
//import java.util.Date;
//import java.util.HashMap;
//import java.util.LinkedHashMap;
//import java.util.Map;
//
///**
// * 自定义异常处理
// *
// * <p>异常时用JSON代替HTML异常信息<p>
// *
// * @author yinjihuan
// *
// */
//public class JsonExceptionHandler extends DefaultErrorWebExceptionHandler {
//
//    public JsonExceptionHandler(ErrorAttributes errorAttributes, ResourceProperties resourceProperties,
//                                ErrorProperties errorProperties, ApplicationContext applicationContext) {
//        super(errorAttributes, resourceProperties, errorProperties, applicationContext);
//    }
////    @Override
////    public Map<String, Object> getErrorAttributes(WebRequest webRequest, ErrorAttributeOptions options) {
////        Map<String, Object> map = super.getErrorAttributes(webRequest, options);
////        map.put("company","atguigu");
////        //我们的异常处理器携带的数据
////        Map<String,Object> ext = (Map<String,Object>)webRequest.getAttribute("ext", 0);
////        System.out.println(map.put("ext", ext));
////        return map;
////    }
//// 帮我们在页面共享信息；
//@Override
//public Map<String, Object> getErrorAttributes(RequestAttributes requestAttributes,
//                                              boolean includeStackTrace) {
//    Map<String, Object> errorAttributes = new LinkedHashMap<String, Object>();
//    errorAttributes.put("timestamp", new Date());
////    addStatus(errorAttributes, requestAttributes);
////    addErrorDetails(errorAttributes, requestAttributes, includeStackTrace);
////    addPath(errorAttributes, requestAttributes);
//    return errorAttributes;
//}
//    /**
//     * 获取异常属性
//     */
//    @Override
//    protected Map<String, Object> getErrorAttributes(ServerRequest request, boolean includeStackTrace) {
////        Map<String, Object> map = new HashMap<>();
////        map.put("success", false);
////        map.put("code", 20005);
////        map.put("message", "网关失败");
////        map.put("data", null);
////        return map;
//        int code = 5001;
//        Throwable error = super.getError(request);
//        if (error instanceof org.springframework.cloud.gateway.support.NotFoundException) {
//            code = 5001;
//        }
//        return response(code, this.buildMessage(request, error));
//
//    }
//
//    /**
//     * 指定响应处理方法为JSON处理的方法
//     * @param errorAttributes
//     */
//    @Override
//    protected RouterFunction<ServerResponse> getRoutingFunction(ErrorAttributes errorAttributes) {
//        return RouterFunctions.route(RequestPredicates.all(), this::renderErrorResponse);
//    }
//
//    /**
//     * 根据code获取对应的HttpStatus
//     * @param errorAttributes
//     */
//    @Override
//    protected int getHttpStatus(Map<String, Object> errorAttributes) {
//        int statusCode = (int) errorAttributes.get("status");
//        return 600;
//    }
//
//    /**
//     * 构建异常信息
//     * @param request
//     * @param ex
//     * @return
//     */
//    private String buildMessage(ServerRequest request, Throwable ex) {
//        StringBuilder message = new StringBuilder("Failed to handle request [");
//        message.append(request.methodName());
//        message.append(" ");
//        message.append(request.uri());
//        message.append("]");
//        if (ex != null) {
//            message.append(": ");
//            message.append(ex.getMessage());
//        }
//        return message.toString();
//    }
//
//    /**
//     * 构建返回的JSON数据格式
//     * @param status        状态码
//     * @param errorMessage  异常信息
//     * @return
//     */
//    public static Map<String, Object> response(int status, String errorMessage) {
//        Map<String, Object> map = new HashMap<>();
//        map.put("code", status);
//        map.put("message", errorMessage);
//        map.put("data", null);
//        return map;
//    }
//}